/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Store;

import RoomPanel.userInfo.DisplayUserInfo;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.String;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import players.PlayersEnum;

/**
 *
 * @author vicky cheung
 */
public class StorePage extends HttpServlet {

    private final String connectionString = "jdbc:sqlserver://w2ksa.cs.cityu.edu.hk:1433;databaseName=aiad048_db;"; 
    private final String conACPW = "aiad048";
    private int playerPage = 1;
    private int photoCount = 8;
    private String pNum;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();   
        
        
        
        HttpSession session = request.getSession();
        session.setAttribute("PPage", 1); 
        String action = request.getParameter("action");
        
            out.println("<html>");
            out.println("<head>");
            out.println("<script type='text/javascript'>");
            out.println("function nextPage() { document.getElementById('playerPage').value++; document.getElementById('PpageNum').value++;}");
            out.println("function nextPage() { document.getElementById('playerPage').value--; document.getElementById('PpageNum').value--;}");
            out.println("</script>");
            out.println("<title>Servlet StorePage</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet StorePage</h1>");
            out.println("<div>");
            
            //a box for jumping to a specific page
            out.println("<form method=\"get\">");
            out.println("Page: ");
            out.println("<input type=\"text\" name=\"PpageNum\" value=\"\">"); 
            out.println("/6");
            out.println("<input type=\"Submit\" value=\"Jump to\">");
            out.println("</form>");
            //end for the box
            
            out.println("<button type='button' action='player_button'>Player</button>");
            out.println("<button type='button' action='tactics_button'>Tactics</button>");
            out.println("<button type='button' action='yours_button'>Your Player</button>");
            out.println("</div>");
            if("7".equals(request.getParameter("PpageNum"))) {
                displayTactics(request, response);
            }
            displayPlayer(request, response);

                
        if (action != null) {
            // call different action depends on the action parameter
            if (action.equalsIgnoreCase("player_button")) {
                this.displayPlayer(request, response);
            } else if (action.equalsIgnoreCase("tactics_button")) {
                this.displayTactics(request, response);
            } else if (action.equalsIgnoreCase("yours_button")) {
                this.displayYours(request, response);
            }
        }
        
        //if(Integer.parseInt(request.getParameter("PpageNum")) == 1)
        //if( session.getAttribute("PPage")==1 )
        // if (pNum == null)   
        //    pNum=request.getParameter("PpageNum");
        //    displayPlayer(request, response);
        
    }
        
    protected void displayTactics(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException {  
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            /*
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource) envCtx.lookup("jdbc/FootballManager");
            Connection con = ds.getConnection();
            Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            String sql = String.format("SELECT * FROM Tactics");
            ResultSet rsT = stmt.executeQuery(sql);
            rsT.next();
            */
            
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(connectionString,conACPW ,conACPW);
            Statement stmt = con.createStatement();
            String strSQL = "Select * from Tactics";
            ResultSet rsT = stmt.executeQuery(strSQL);
            //int numRow = countRow(rsT);
            //rsT.next();
            
            
            out.println("<Table>");
            while (rsT != null && rsT.next() != false) {
                String Tname = rsT.getString("name"); //tactics name 
                int Front = rsT.getInt("front");
                int Middle = rsT.getInt("middle");
                int Back = rsT.getInt("back");
                String Description = rsT.getString("description");
                int price = rsT.getInt("price");
                Tactics t;
                
                out.println("<tr>");
                out.println("<td style='border-style:solid; border-width:5px;'> ");
                out.println("<img src ='" + Tactics.getTacticsImageURLByName(Tname) + "'></img>"); //public function getPlayerImageURLByName -> only classname needed to call the function
                out.println("<img style='float:left' src='http://db.tt/cxZYC2vq'/ >");
                out.println("</td>");
                out.println("<td width=150px height=150px style='border-style:solid; border-width:5px;'>");
                out.println("Price: " + price + " <br>");
                out.println("Front: " + Front + " <br>");
                out.println("Middle: " + Middle + " <br>");
                out.println("Back: " + Back + " <br>");
                out.println(Description);
                out.println("</td>");
                out.println("</tr>");
            }
            out.println("</Table>");
            out.println("</body>");
            out.println("</html>");
           
        }catch (SQLException e) {
            out.println("<div style='color: red'>" + e.toString() + "</div>");
        } finally {            
            out.close();
        }
    }
    
    
    protected void displayPlayer(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException {
         response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<div"); //id='playerPage'
            out.println("<form>");
            out.println("<input type=\"hidden\" name=\"playerPage\" value=\"1\">");
            out.println("</form>");
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(connectionString,conACPW ,conACPW);
            Statement stmt = con.createStatement();
            String strSQL = "Select * from Player";
            ResultSet rsP = stmt.executeQuery(strSQL);
            int Pid;
            String Pname;
            int imgCount=0;
            out.println(request.getParameter("PpageNum"));
            //out.println("<p>11111</p>");
            //if(Integer.parseInt(request.getParameter("PpageNum")) == 1)
            
            if((request.getParameter("PpageNum")) == null || "1".equals(request.getParameter("PpageNum")))
            {
                out.println("<tr>");
                while(rsP.next() && rsP!= null)
                {
                    Pid = rsP.getInt("playerID");
                    Pname = rsP.getString("name");
                    out.println("<td>");
                    out.println("<p>" + Pname + "</p>");
                    out.println("<tr> <img src ='" + PlayersEnum.getCardURLByName(Pname) + "' />"); //public function getCardURLByName -> only classname needed to call the function
                    //out.println("<p>" + Pid + "</p>");
                    out.println("<img src='http://db.tt/cxZYC2vq'/>");
                    out.println("</td>");
                    imgCount++;
                }
                 out.println("<img onclick='nextPage()' alt='next button' style='float: right' width=150px height=150px src = 'http://db.tt/VuvQjHCV'/ >");
            }
            
            if("2".equals(request.getParameter("PpageNum")))
            {
                out.println("<tr>");
                Pid = rsP.getInt("playerID");
                while(rsP.next() && rsP!= null && imgCount<4 && Pid >4 && Pid<=8)
                {
                    Pname = rsP.getString("name");
                    out.println("<td>");
                    out.println("<p>" + Pname + "</p>");
                    out.println("<tr> <img src ='" + PlayersEnum.getCardURLByName(Pname) + "' />"); //public function getCardURLByName -> only classname needed to call the function
                    //out.println("<p>" + Pid + "</p>");
                    out.println("<img src='http://db.tt/cxZYC2vq'/>");
                    out.println("</td>");
                    imgCount++;
                }
                 out.println("<img onclick='nextPage()' alt='next button' style='float: right' width=150px height=150px src = 'http://db.tt/VuvQjHCV'/ >");
            }
            
            if("3".equals(request.getParameter("PpageNum")))
            {
                out.println("<tr>");
                Pid = rsP.getInt("playerID");
                while(rsP.next() && rsP!= null && imgCount<4 && Pid >8 && Pid<=12)
                {
                    Pname = rsP.getString("name");
                    out.println("<td>");
                    out.println("<p>" + Pname + "</p>");
                    out.println("<tr> <img src ='" + PlayersEnum.getCardURLByName(Pname) + "' />"); //public function getCardURLByName -> only classname needed to call the function
                    //out.println("<p>" + Pid + "</p>");
                    out.println("<img src='http://db.tt/cxZYC2vq'/>");
                    out.println("</td>");
                    imgCount++;
                }
                 out.println("<img onclick='nextPage()' alt='next button' style='float: right' width=150px height=150px src = 'http://db.tt/VuvQjHCV'/ >");
            }
            
            if("4".equals(request.getParameter("PpageNum")))
            {
                out.println("<tr>");
                Pid = rsP.getInt("playerID");
                while(rsP.next() && rsP!= null && imgCount<4 && Pid >12 && Pid<=16)
                {
                    Pname = rsP.getString("name");
                    out.println("<td>");
                    out.println("<p>" + Pname + "</p>");
                    out.println("<tr> <img src ='" + PlayersEnum.getCardURLByName(Pname) + "' />"); //public function getCardURLByName -> only classname needed to call the function
                    //out.println("<p>" + Pid + "</p>");
                    out.println("<img src='http://db.tt/cxZYC2vq'/>");
                    out.println("</td>");
                    imgCount++;
                }
                 out.println("<img onclick='nextPage()' alt='next button' style='float: right' width=150px height=150px src = 'http://db.tt/VuvQjHCV'/ >");
            }
            
            if("5".equals(request.getParameter("PpageNum")))
            {
                out.println("<tr>");
                Pid = rsP.getInt("playerID");
                while(rsP.next() && rsP!= null && imgCount<4 && Pid >16 && Pid<=20)
                {
                    Pname = rsP.getString("name");
                    out.println("<td>");
                    out.println("<p>" + Pname + "</p>");
                    out.println("<tr> <img src ='" + PlayersEnum.getCardURLByName(Pname) + "' />"); //public function getCardURLByName -> only classname needed to call the function
                    //out.println("<p>" + Pid + "</p>");
                    out.println("<img src='http://db.tt/cxZYC2vq'/>");
                    out.println("</td>");
                    imgCount++;
                }
                 out.println("<img onclick='nextPage()' alt='next button' style='float: right' width=150px height=150px src = 'http://db.tt/VuvQjHCV'/ >");
            }
            
            if("6".equals(request.getParameter("PpageNum")))
            {
                out.println("<tr>");
                Pid = rsP.getInt("playerID");
                while(rsP.next() && rsP!= null && imgCount<4 && Pid >20 && Pid<=24)
                {
                    Pname = rsP.getString("name");
                    out.println("<td>");
                    out.println("<p>" + Pname + "</p>");
                    out.println("<tr> <img src ='" + PlayersEnum.getCardURLByName(Pname) + "' />"); //public function getCardURLByName -> only classname needed to call the function
                    //out.println("<p>" + Pid + "</p>");
                    out.println("<img src='http://db.tt/cxZYC2vq'/>");
                    out.println("</td>");
                    imgCount++;
                }
                 out.println("<img onclick='nextPage()' alt='next button' style='float: right' width=150px height=150px src = 'http://db.tt/VuvQjHCV'/ >");
            }
            
            
            //rsP.next();
            //int numRow = countRow(rsP);
            
            /*
            if(playerPage == 1)
            {
                
                out.println("<Table>");
                out.println("<tr>");
                //int i=0;
                
                while (rsP.next())
                {
                    Pid = rsP.getInt("playerID");
                    Pname = rsP.getString("name");
                    PlayersEnum p;
                    out.println("<tr> <img src ='" + PlayersEnum.getCardURLByName(Pname) + " />"); //public function getCardURLByName -> only classname needed to call the function
                    out.println("<img src ='http://db.tt/nZcg5ADG' />");
                    out.println("</tr>");
                }
                
                /*
                while (rsP != null && rsP.next() != false && Pid > 0 && Pid <= photoCount/2 && i<photoCount/2) {
                    Pname = rsP.getString("name");
                    PlayersEnum p; 
                        out.println("<td> <img src ='" + PlayersEnum.getCardURLByName(Pname) + "'></img>"); //public function getCardURLByName -> only classname needed to call the function
                        out.println("<img src ='http://db.tt/nZcg5ADG'></img>");
                        out.println("</td>");
                        i++;
                }
                out.println("</tr>");   

                while (rsP != null && rsP.next() && Pid > photoCount/2 && Pid <= photoCount/2+4 && i<photoCount) {
                    Pname = rsP.getString("name");
                    PlayersEnum p; 
                        out.println("<td> <img src ='" + PlayersEnum.getCardURLByName(Pname) + "'></img>"); //public function getCardURLByName -> only classname needed to call the function
                        out.println("<img src ='http://db.tt/nZcg5ADG'></img>");
                        out.println("</td>");
                        i++;
                }
                out.println("</tr>"); 
                
                
                out.println("</Table>");
                playerPage ++;
                out.println("<img src='http://db.tt/VuvQjHCV'>"); //next button
            }
            /*
            if(playerPage == 2)
            {
                out.println("<Table>");
                out.println("<tr>");
                int j=0;
                int Pid = rsP.getInt("playerID");
                while (rsP != null && rsP.next() != false && Pid > photoCount+1 && Pid <= 2*photoCount-4 && j<photoCount/2) {
                    Pname = rsP.getString("name");
                    PlayersEnum p; 
                        out.println("<td> <img s rc ='" + PlayersEnum.getCardURLByName(Pname) + "'></img>"); //public function getCardURLByName -> only classname needed to call the function
                        out.println("<img src ='http://db.tt/nZcg5ADG'></img>");
                        out.println("</td>");
                        j++;
                }
                out.println("</tr>");   

                while (rsP != null && rsP.next() != false && Pid > 2*photoCount-4 && Pid <= 2*photoCount && j<photoCount) {
                    Pname = rsP.getString("name");
                    PlayersEnum p; 
                        out.println("<td> <img src ='" + PlayersEnum.getCardURLByName(Pname) + "'></img>"); //public function getCardURLByName -> only classname needed to call the function
                        out.println("<img src ='http://db.tt/nZcg5ADG'></img>");
                        out.println("</td>");
                        j++;
                }
                out.println("</tr>"); 
                out.println("</Table>");
                playerPage ++;
                out.println("<img src='http://db.tt/VuvQjHCV'>"); //next button
                out.println("<img src='http://db.tt/DLdy3Y2p'>"); //back button
            }
            
            if(playerPage == 3)
            {
                int i=0;
                out.println("<Table>");
                out.println("<tr>");
                int k=0;
                int Pid = rsP.getInt("playerID");
                while (rsP != null && rsP.next() != false && i<photoCount/2 && Pid > 2*photoCount  && Pid <= 2*photoCount + photoCount/2) {
                    Pname = rsP.getString("name");
                    PlayersEnum p; 
                        out.println("<td> <img s rc ='" + PlayersEnum.getCardURLByName(Pname) + "'></img>"); //public function getCardURLByName -> only classname needed to call the function
                        out.println("<img src ='http://db.tt/nZcg5ADG'></img>");
                        out.println("</td>");
                        i++;
                }
                out.println("</tr>"); 
                out.println("</Table>");
                out.println("<img src='http://db.tt/DLdy3Y2p'>");  // use javascript to update the page number
                
            }  
            */
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
        } catch (SQLException e) {
            out.println("<div style='color: red'>" + e.toString() + "</div>");
        } finally{            
            out.close();
        }
    }
    
    protected void displayYours(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    }
    
    private int countRow(ResultSet rs) throws SQLException {
    int result = 0;
    if (rs != null && rs.last() != false) {
        result = rs.getRow();
        rs.beforeFirst();
    }
    return result;
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(StorePage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(StorePage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
